package com.itheima.app.manager;

import cn.hutool.core.util.RandomUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.im.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.UserLike;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.UserLikeSrevice;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserLikeManager {

    @DubboReference
    UserLikeSrevice userLikeSrevice;

    @DubboReference
    UserInfoService userInfoService;


    @DubboReference
    FriendService friendService;

    @Autowired
    HuanXinTemplate huanXinTemplate;

    public UserLikeCountVo counts() {
        Long userId = UserHolder.get().getId();
        return userLikeSrevice.counts(userId);

    }

    public ResponseEntity LikeByPage(String type, Integer pageNum, Integer pageSize) {
        PageBeanVo vo = userLikeSrevice.LikeByPage(type, UserHolder.get().getId(), pageNum, pageSize);
        List<Long> items = (List<Long>) vo.getItems();
        List<UserLikeVo> userLikeList = new ArrayList<>();
        items.forEach(item -> {
            UserInfo userInfoById = userInfoService.findByUserId(item);
            UserLikeVo userLikeVo = new UserLikeVo();
            userLikeVo.setId(userInfoById.getId());//id
            userLikeVo.setAvatar(userInfoById.getAvatar());//头像
            userLikeVo.setNickname(userInfoById.getNickname());//昵称
            userLikeVo.setGender(userInfoById.getGender());//性别
            userLikeVo.setAge(userInfoById.getAge());//年龄
            userLikeVo.setCity(userInfoById.getCity());//城市
            userLikeVo.setEducation(userInfoById.getEducation());//学历
            userLikeVo.setMarriage(userInfoById.getMarriage());//婚姻状况
            userLikeVo.setMatchRate(RandomUtil.randomInt(33, 99));
            userLikeVo.setAlreadyLove(false);
            userLikeList.add(userLikeVo);
        });
        vo.setItems(userLikeList);
        return ResponseEntity.ok(vo);
    }

    //粉丝 - 喜欢
    public void like(String uid) {
        Long friendId = Long.valueOf(uid);
        friendService.addContact(UserHolder.get().getId(),friendId);
        //调用环信模版添加好友
        huanXinTemplate.addContacts("HX" + UserHolder.get().getId(), "HX" + friendId);
    }

    // 探花-不喜欢
    public void removeLove(Long likeUserId) {
        // 1. 获取线程中的userId
        Long userId = UserHolder.get().getId();
        // 2. 调用rpc删除喜欢  并判断对方是否喜欢自己
        Boolean removeLove = userLikeSrevice.removeLove(userId, likeUserId);
        // 3. 判断
        if (removeLove) {
            // 4. 说明对方喜欢着自己 删除喜欢时候也要删除掉好友关系
            friendService.removeContacts(userId, likeUserId);
            // 5. 删除掉环信上的好友
            huanXinTemplate.deleteContacts("HX" + userId, "HX" + likeUserId);
        }
        // 6. 删除推荐列表内的数据
        userLikeSrevice.remove(userId, likeUserId);
    }
}
